#include "readIni.h"
#include <fstream>
#include <iostream>


namespace Jxiepc {


/** 去除字符串中的所有空格 */
static const std::string& stripAll(std::string &str) {
    int index = 0;
    if(!str.empty())
    {
        while( (index = str.find(' ',index)) != std::string::npos)
        {
            str.erase(index, 1);
        }
    }
 
    return str;
}

/** 去除左右边的空格 */
static const std::string& strip(std::string& str) {
     if (str.empty())
    {
        return str;
    }

    str.erase(0,str.find_first_not_of(" "));
    str.erase(str.find_last_not_of(" ") + 1);

    return str;
}

/** 去除右边的空格 */
static const std::string& strip_right(std::string& str) {
     if (str.empty())
    {
        return str;
    }

    //str.erase(0,str.find_first_not_of(" "));
    str.erase(str.find_last_not_of(" ") + 1);

    return str;
}


ConfigIni::ConfigIni(const std::string& filename) 
    :m_filename(filename){
    std::ifstream ifs(filename.c_str(), std::ios::binary); 
    
    if(!ifs) {
        std::cout << " open error" << std::endl;
        exit(-1);
    }
    std::string line;
    std::string key, kkey, val;
    bool isContinue = false;
    bool isContinue_kkey = false;
    int pos;
    while(getline(ifs, line))
	{
        // 判断是否为空行
        if(line == "") continue;
        // 判断是否为注释
        if(line[0] == '#') continue;

        // 判断是否为节
        if(line[0] == '[') {
            line = strip(line);
            key = line.substr(1, line.size()-2);
            key = strip(key);
            std::cout << "[" << key << "]=UNDEF" << std::endl; 
        // 处理键值
        }else {
            line = strip_right(line);
            if(isContinue) {
                if(line[line.size()-1] == '\\') {
                    val += line.substr(0, line.size()-1);
                    continue;
                }else {
                    val += line;
                    isContinue = false;
                }
            }else {
                pos = line.find("=");
                if(pos == std::string::npos) {
                    if(line[line.size() - 1] == '\\') {
                        kkey = line.substr(0, line.size()-1);
                        isContinue_kkey = true;
                        continue; 
                    }else {
                        std::cout << "[error]: " << kkey << std::endl;
                        continue;
                    }
                }
                if(isContinue_kkey){
                    kkey += line.substr(0, pos);
                    isContinue_kkey = false;
                }else {
                    kkey = line.substr(0, pos);
                }
                kkey = strip(kkey);

                val = line.substr(pos + 1, line.size()-pos); 
                if(val[val.size() - 1] == '\\') {
                    isContinue = true;
                    val = val.substr(0, val.size()-1);
                    continue; 
                }
            }
            val = strip(val);
            do {
                /** 处理单引号 */
                if(val[0] == '\''){
                    if(val.size() != 2 && val == std::string(val.size(), '\'')) 
                        break;
                    pos = val.find('\'', 1);
                    if(pos != std::string::npos) {
                        val = val.substr(1, pos-1);
                        break;
                    }
                }
                /** 处理双引号 */
                if(val[0] == '\"'){
                    if(val.size() != 2 && val == std::string(val.size(), '\"')) 
                        break;
                    pos = val.find('\"', 1);
                    if(pos != std::string::npos) {
                        val = val.substr(1, pos-1);
                        break;
                    }
                }
                /** 处理注释 */
                pos = val.find(';');
                if(pos != std::string::npos) {
                    val = val.substr(0, pos); 
                }
                pos = val.find('#');
                if(pos != std::string::npos) {
                    val = val.substr(0, pos); 
                }
            }while(0);

            val = strip_right(val);
            m_dict[key][kkey] = val;
            std::cout << "[" << key << ":" << kkey << "]=[" << val << "]" << std::endl;
        }
	}
}

ConfigIni::~ConfigIni() {

}

}
